<?php include "../public/header.php"?>
<?php include "../public/header_public.php"?>
<?php include "../public/header_edit.php"?>
<script src="/static/admin/js/math.js"></script>
<link rel="stylesheet" type="text/css" href="/static/admin/css/main.css">
<style>

</style>
<body class="laytp-container">
<div style="height: 45px"></div>
<div id="opt_nav">
</div>
<div id="dialog">
    <div class="page-title" id="page-title">
        <div class="page-title-con">
            <span class="title" id="titss">设置含量</span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">确定</button>
                <span id="page-close" class="page-close" onclick="layer_close()">×</span>
            </div>
        </div>
    </div>
</div>

<div class="layui-card" id="table-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <div class="layui-col-md12">
                <div class="layui-form layui-form-pane" id="commentForm">
                    <div class="main-container" style="margin:0!important;padding: 0 10px!important;">
                        <div class="layui-form-item height_auto" style="padding: 0!important;">
                            <div class="short_table_list" key="1">
                                <table class="layui-table" lay-size="sm" id="short_tables">
                                    <thead>
                                    <tr style="height: 35px!important">
                                        <th width="45">序号</th>
                                        <th>项目名称分类</th>
                                        <th>项目名称</th>
                                        <th>项目特征描述</th>
                                        <th>计量单位</th>
                                        <th>工程量</th>
                                        <th>含量</th>
                                    </tr>
                                    </thead>
                                    <tbody class="short_table_tbody" id="list_list" style="min-height: 300px;max-height: 600px"></tbody>
                                </table>
                                <div class="listerror">提示：<span></span></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;

        var str = '';
        $('.opTable-open-td tr', parent.document).each(function (item,key) {
            if($(this).find('td').eq(1).find('div>div').attr('class') == 'layui-unselect layui-form-checkbox layui-form-checked'){
                var enname = $(this).find('td').eq(2).find('div').text();
                var enfeat = $(this).find('td').eq(3).find('div').text();
                var enunit = $(this).find('td').eq(4).find('div').text();
                var enword = $(this).find('td').eq(5).find('div').text();
                var han = $(this).find('td').eq(6).find('div').text() || 1;
                var id = $(this).find('td').eq(7).find('div').text();

                var type = $(this).parents('table').parents('tr').prev().find('td[data-field="enname"] div').text();

                str += '<tr>';
                str += '<td style="width: 45px">1</td>';
                str += '<td style="padding: 0 10px!important;">'+type+'</td>';
                str += '<td style="padding: 0 10px!important;">'+enname+'</td>';
                str += '<td style="padding: 0 10px!important;">'+enfeat+'</td>';
                str += '<td style="padding: 0 10px!important;">'+enunit+'</td>';
                str += '<td style="padding: 0 10px!important;">'+enword+'</td>';
                str += '<td><input type="number" class="layui-input han" key="'+id+'" enword="'+enword+'" value="'+han+'"></td>';
                str += '</tr>';

            }
        });
        $('#list_list').html(str);
        update_refresh_row_func();


        form.on('submit(add)', function(data){
            var list = [];
            var enword_all = 0;
            try {
                $('#list_list tr').each(function (i) {
                    var han = $(this).find('.han').val() || 0;
                    var id = $(this).find('.han').attr('key');
                    var enword = parseFloat($(this).find('.han').attr('enword') || 0) || 0;
                    list.push( {
                        han: han,
                        id: id,
                    });
                    enword_all += parseFloat(han) * parseFloat(enword);
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();






            $('#list_list tr', parent.parent.document).eq(parent.param_index).find('.'+parent.param_type).val(enword_all.toFixed(2));
            $('#list_list tr', parent.parent.document).eq(parent.param_index).find('.'+parent.param_type).attr('list',JSON.stringify(list));
            parent.parent.jisuan_han();
            parent.layer_close();
            return false;
        });
    });





    function saveinfo() {
        var buttype = $("#type_content .active").attr('key');
        var list = [''],repeat = [];
        try {
            $('#list_list tr').each(function (i) {
                var type = buttype;
                var basetype = $(this).find('.basetype').val();
                var name = $(this).find('.name').val();
                var listid = $(this).find('.name').attr('listid') || 0;
                var source = $(this).find('.name').attr('source') || 0;
                var guige = $(this).find('.guige').val();
                var valunit = $(this).find('.valunit').val();
                var content_exp = $(this).find('.content_exp').val();
                var content = $(this).find('.content').val();
                var loss_rate = $(this).find('.loss_rate').val();
                var no_price = $(this).find('.no_price').val();
                var price = $(this).find('.price').val();
                var explain = $(this).find('.explain').val();

                var index = i + 1;
                if(!name){
                    throw new Error('第'+index+'行，请填写名称');
                }
                if(!valunit){
                    throw new Error('第'+index+'行，请填写单位');
                }
                if(!no_price){
                    throw new Error('第'+index+'行，请填写不含税单价');
                }
                if(!price){
                    throw new Error('第'+index+'行，请填写成本单价');
                }

                var str = type+'-'+basetype+'-'+name+'-'+guige+'-'+valunit;
                if($.inArray(str, repeat) != -1){
                    throw new Error('第'+index+'行，内容重复');
                }
                repeat.push(str);

                list.push({
                    type:type,
                    basetype:basetype,
                    name:name,
                    id:listid,
                    source:source,
                    guige:guige,
                    valunit:valunit,
                    content_exp:content_exp,
                    content:content,
                    loss_rate:loss_rate,
                    no_price:no_price,
                    price:price,
                    explain:explain,
                    enginid: param_enginid,
                });
            });
        } catch(e) {
            $(".listerror").find('span').text(e.message);
            $(".listerror").show();
            return false;
        }
        $(".listerror").hide();

        facade.ajax({url: ask_main_mark_edit, data:{list:list,enginid: param_enginid,type:buttype}}).done(function(res){
            if (res.code === 2000) {
                facade.success("提交成功!");
                setTimeout(function () {
                    window.location.reload();
                },1000)
            }
        });
    }


</script>
</body>
</html>
